package com.xinchuang.service.targetSystem.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.subTargetAnalysis.*;
import com.xinchuang.entity.targetSystem.system.PostureType;
import com.xinchuang.entity.targetSystem.target.*;
import com.xinchuang.mapper.subTargetAnalysis.SubTargetAnalysisDynamicMapper;
import com.xinchuang.mapper.subTargetAnalysis.SubTargetAnalysisIntentMapper;
import com.xinchuang.mapper.subTargetAnalysis.SubTargetAnalysisRegularMapper;
import com.xinchuang.mapper.targetSystem.target.*;
import com.xinchuang.pojoUtil.TargetUtil;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.subTargetAnalysis.*;
import com.xinchuang.service.targetSystem.SubTargetAnalysisService;
import com.xinchuang.service.targetSystem.TargetService;
import com.xinchuang.service.targetSystem.target.PostureComputeTypeService;
import com.xinchuang.util.RedisUtil;
import com.xinchuang.utils.RedisName;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service(value = "subTargetAnalysisService")
public class SubTargetAnalysisServiceImpl implements SubTargetAnalysisService {

    @Value("${importantScore.important}")
    private double important;

    @Value("${importantScore.follow}")
    private double follow;

    @Resource
    private RedisUseService redisUseService;

    @Resource
    private TargetService targetService;

    @Resource
    private PostureComputeTypeService postureComputeTypeService;

    @Resource
    private PostureTypeMapper postureTypeMapper;

    @Resource
    private TargetAnalyseTableMapper targetAnalyseTableMapper;

    @Resource
    private ISubTargetAnalysisStaticService subTargetAnalysisStaticService;

    @Resource
    private ISubTargetAnalysisRegularService subTargetAnalysisRegularService;

    @Resource
    private ISubTargetAnalysisDynamicService subTargetAnalysisDynamicService;

    @Resource
    private ISubTargetAnalysisIntentService subTargetAnalysisIntentService;

    @Resource
    private ISubTargetAnalysisRelateService subTargetAnalysisRelateService;

    @Resource
    private SubTargetAnalysisRegularMapper subTargetAnalysisRegularMapper;

    @Resource
    private SubTargetAnalysisDynamicMapper subTargetAnalysisDynamicMapper;

    @Resource
    private SubTargetAnalysisIntentMapper subTargetAnalysisIntentMapper;

    @Resource
    private TargetSubMsgTableMapper targetSubMsgTableMapper;

    @Resource
    private TargetRelationshipMapper targetRelationshipMapper;

    @Resource
    private TargetSubRelationTableMapper targetSubRelationTableMapper;

    @Resource
    private SystemIndexMapper systemIndexMapper;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Result getSubTargetAnalysisStaticList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");

        JSONObject subTargetAnalysisStaticData = getSubTargetAnalysisStaticData(targetId);
//        JSONObject subTargetAnalysisStaticData;
//        SubTargetAnalysisStatic subTargetAnalysisStatic = subTargetAnalysisStaticService.getOne(new LambdaQueryWrapper<SubTargetAnalysisStatic>()
//                .eq(SubTargetAnalysisStatic::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisStatic::getContent)
//        );
//        if (subTargetAnalysisStatic == null) {
//            subTargetAnalysisStaticData = getSubTargetAnalysisStaticData(targetId);
//        } else {
//            subTargetAnalysisStaticData = JSON.parseObject(subTargetAnalysisStatic.getContent());
//        }

        JSONArray chart = subTargetAnalysisStaticData.getJSONArray("chart");
        //表头
        JSONArray index = subTargetAnalysisStaticData.getJSONArray("header");
        //表身
        JSONArray dataList = subTargetAnalysisStaticData.getJSONArray("dataList");
        List<JSONObject> jsonArray = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            jsonArray.add(dataList.getJSONObject(i));
        }

        JSONArray table = new JSONArray();
        JSONArray table0 = new JSONArray();
        table0.add("子目标");
        for (int i = 0; i < index.size(); i++) {
            table0.add(index.getString(i));
        }
        table0.add("分值");
        table0.add("排序");
        table.add(table0);

        jsonArray.sort(Comparator.comparing(j -> j.getDoubleValue("score")));
        Collections.reverse(jsonArray);

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject item = jsonArray.get(i);
            JSONArray tableI = new JSONArray();
            tableI.add(item.getString("name"));
            for (int j = 0; j < index.size(); j++) {
                tableI.add(item.getDoubleValue(index.getString(j)));
            }
            double score = item.getDoubleValue("score");
            tableI.add(score);
            tableI.add(i + 1);
            table.add(tableI);
        }

        Map r = new HashMap();
        r.put("chart", chart);
        r.put("table", table);
        return ResultUtil.success(r);
    }

    private JSONObject getSubTargetAnalysisStaticData(String targetId) {
        List<JSONObject> jsonArray = new ArrayList<>();

        int importantCount = 0;
        int followCount = 0;
        int generalCount = 0;
        //指标
        Set<String> index = new HashSet();
        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();

            JSONObject j = new JSONObject();
            j.put("id", subId);
            j.put("name", targetName);

            List<SystemIndex> systemIndexList = systemIndexMapper.selectList(new LambdaQueryWrapper<SystemIndex>()
                    .eq(SystemIndex::getRedId, subId)
                    .eq(SystemIndex::getLevel, "1")
                    .eq(SystemIndex::getType, "ununder")
            );
            double score = 0;
            for (SystemIndex systemIndex : systemIndexList) {
                String indicatorName = systemIndex.getIndicatorName();
                String indicatorValue = systemIndex.getIndicatorValue();
                if (StringUtils.isNotEmpty(indicatorValue)) {
                    score += Double.parseDouble(indicatorValue);
                }
                j.put(indicatorName, indicatorValue);
                index.add(indicatorName);
            }
            if (score > important) {
                importantCount++;
            } else if (score > follow) {
                followCount++;
            } else {
                generalCount++;
            }
            j.put("score", score);
            jsonArray.add(j);
        }

        JSONArray chart = new JSONArray();
        JSONObject importantJSONObject = new JSONObject();
        importantJSONObject.put("name", "重要");
        importantJSONObject.put("value", importantCount);
        JSONObject followJSONObject = new JSONObject();
        followJSONObject.put("name", "关注");
        followJSONObject.put("value", followCount);
        JSONObject generalJSONObject = new JSONObject();
        generalJSONObject.put("name", "一般");
        generalJSONObject.put("value", generalCount);
        chart.add(importantJSONObject);
        chart.add(followJSONObject);
        chart.add(generalJSONObject);

        JSONObject insert = new JSONObject();
        insert.put("header", index);
        insert.put("dataList", jsonArray);
        insert.put("chart", chart);
        SubTargetAnalysisStatic subTargetAnalysisStatic = new SubTargetAnalysisStatic();
        subTargetAnalysisStatic.setTargetId(targetId);
        subTargetAnalysisStatic.setContent(insert.toJSONString());
        subTargetAnalysisStaticService.saveOrUpdate(subTargetAnalysisStatic, new LambdaUpdateWrapper<SubTargetAnalysisStatic>()
                .eq(SubTargetAnalysisStatic::getTargetId, targetId));
        return insert;
    }

    @Override
    public Result getStayTargetList(JSONObject jsonObject) {
        List<PostureType> postureTypeList = postureTypeMapper.selectList(new LambdaQueryWrapper<>());
        long count = postureTypeList.stream()
                .filter(postureType -> postureType.getParentId().equals("0")).count();
        if (count != 1) {
            return ResultUtil.error(500, "根节点不止一个");
        }
        Optional<PostureType> first = postureTypeList.stream()
                .filter(postureType -> postureType.getParentId().equals("0")).findFirst();
        List<PostureTypeVO> list;
        if (first.isPresent()) {
            String typeId = first.get().getTypeId();
            list = getChildren(typeId, postureTypeList);
        } else {
            list = new ArrayList<>();
        }
        return ResultUtil.success(list);
    }

    @Override
    public Result uploadFightPosition(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        String position = jsonObject.getString("position");
        TargetAnalyseTable targetAnalyseTable = targetAnalyseTableMapper.selectOne(new LambdaQueryWrapper<TargetAnalyseTable>()
                .eq(TargetAnalyseTable::getRedId, targetId));
        int i;
        if (null != targetAnalyseTable) {
            targetAnalyseTable.setFightPosition(position);
            i = targetAnalyseTableMapper.updateById(targetAnalyseTable);
        } else {
            TargetAnalyseTable targetAnalyseTable1 = new TargetAnalyseTable();
            targetAnalyseTable1.setRedId(targetId);
            targetAnalyseTable1.setFightPosition(position);
            i = targetAnalyseTableMapper.insert(targetAnalyseTable1);
        }
        if (i > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(500, "操作失败");
    }

    @Override
    public Result getSubTargetList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));
        List<Map> result = new ArrayList<>();
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();
            Map<String, String> m = new HashMap<>();
            m.put("id", subId);
            m.put("name", targetName);
            result.add(m);
        }
        return ResultUtil.success(result);
    }

    @Override
    public Result getSubTargetAnalysisTotalList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        jsonObject.put("paramList", new JSONArray());

        //子目标
        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));
        Map<String, String> m4 = new HashMap();
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();
            m4.put(subId, targetName);
        }

        //静态分析
        JSONObject subTargetAnalysisStaticData = getSubTargetAnalysisStaticData(targetId);
//        JSONObject subTargetAnalysisStaticData;
//        SubTargetAnalysisStatic subTargetAnalysisStatic = subTargetAnalysisStaticService.getOne(new LambdaQueryWrapper<SubTargetAnalysisStatic>()
//                .eq(SubTargetAnalysisStatic::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisStatic::getContent)
//        );
//        if (subTargetAnalysisStatic == null) {
//            subTargetAnalysisStaticData = getSubTargetAnalysisStaticData(targetId);
//        } else {
//            subTargetAnalysisStaticData = JSON.parseObject(subTargetAnalysisStatic.getContent());
//        }
        JSONArray indexStatic = subTargetAnalysisStaticData.getJSONArray("header");
        //指标值
        Map<String, JSONObject> staticAnalysis = new HashMap();
        JSONArray dataListStatic = subTargetAnalysisStaticData.getJSONArray("dataList");
        for (int i = 0; i < dataListStatic.size(); i++) {
            JSONObject jsonObject1 = dataListStatic.getJSONObject(i);
            staticAnalysis.put(jsonObject1.getString("name"), jsonObject1);
        }

        //规律分析
        JSONObject subTargetAnalysisRegularData = getSubTargetAnalysisRegularData(jsonObject);
//        JSONObject subTargetAnalysisRegularData;
//        SubTargetAnalysisRegular subTargetAnalysisRegular = subTargetAnalysisRegularService.getOne(new LambdaQueryWrapper<SubTargetAnalysisRegular>()
//                .eq(SubTargetAnalysisRegular::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisRegular::getContent)
//        );
//        if (subTargetAnalysisRegular == null) {
//            subTargetAnalysisRegularData = getSubTargetAnalysisRegularData(jsonObject);
//        } else {
//            subTargetAnalysisRegularData = JSON.parseObject(subTargetAnalysisRegular.getContent());
//        }
        //指标值
        Map<String, JSONObject> regularAnalysis = new HashMap();
        JSONArray dataListRegular = subTargetAnalysisRegularData.getJSONArray("dataList");
        for (int i = 0; i < dataListRegular.size(); i++) {
            JSONObject jsonObject1 = dataListRegular.getJSONObject(i);
            regularAnalysis.put(jsonObject1.getString("target_name"), jsonObject1);
        }

        //动态分析
        JSONObject subTargetAnalysisDynamicData = getSubTargetAnalysisDynamicData(jsonObject);
//        JSONObject subTargetAnalysisDynamicData;
//        SubTargetAnalysisDynamic subTargetAnalysisDynamic = subTargetAnalysisDynamicService.getOne(new LambdaQueryWrapper<SubTargetAnalysisDynamic>()
//                .eq(SubTargetAnalysisDynamic::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisDynamic::getContent)
//        );
//        if (subTargetAnalysisDynamic == null) {
//            subTargetAnalysisDynamicData = getSubTargetAnalysisDynamicData(jsonObject);
//        } else {
//            subTargetAnalysisDynamicData = JSON.parseObject(subTargetAnalysisDynamic.getContent());
//        }
        //指标值
        Map<String, JSONObject> dynamicAnalysis = new HashMap();
        JSONArray dataListDynamic = subTargetAnalysisDynamicData.getJSONArray("dataList");
        for (int i = 0; i < dataListDynamic.size(); i++) {
            JSONObject jsonObject1 = dataListDynamic.getJSONObject(i);
            dynamicAnalysis.put(jsonObject1.getString("target_name"), jsonObject1);
        }

        //意图分析
        JSONObject subTargetAnalysisIntentData = getSubTargetAnalysisIntentData(jsonObject);
//        JSONObject subTargetAnalysisIntentData;
//        SubTargetAnalysisIntent subTargetAnalysisIntent = subTargetAnalysisIntentService.getOne(new LambdaQueryWrapper<SubTargetAnalysisIntent>()
//                .eq(SubTargetAnalysisIntent::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisIntent::getContent)
//        );
//        if (subTargetAnalysisIntent == null) {
//            subTargetAnalysisIntentData = getSubTargetAnalysisIntentData(jsonObject);
//        } else {
//            subTargetAnalysisIntentData = JSON.parseObject(subTargetAnalysisIntent.getContent());
//        }
        //指标值
        Map<String, JSONObject> intentAnalysis = new HashMap();
        JSONArray dataListIntent = subTargetAnalysisIntentData.getJSONArray("dataList");
        for (int i = 0; i < dataListIntent.size(); i++) {
            JSONObject jsonObject1 = dataListIntent.getJSONObject(i);
            intentAnalysis.put(jsonObject1.getString("target_name"), jsonObject1);
        }

        //关联分析
        JSONObject subTargetAnalysisRelateData = getSubTargetAnalysisRelateData(jsonObject);
//        JSONObject subTargetAnalysisRelateData;
//        SubTargetAnalysisRelate subTargetAnalysisRelate = subTargetAnalysisRelateService.getOne(new LambdaQueryWrapper<SubTargetAnalysisRelate>()
//                .eq(SubTargetAnalysisRelate::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisRelate::getContent)
//        );
//        if (subTargetAnalysisRelate == null) {
//            subTargetAnalysisRelateData = getSubTargetAnalysisRelateData(jsonObject);
//        } else {
//            subTargetAnalysisRelateData = JSON.parseObject(subTargetAnalysisRelate.getContent());
//        }
        //关联分数
        JSONObject relateMap = subTargetAnalysisRelateData.getJSONObject("relateMap");
        //用于计算列数
        int maxSize = subTargetAnalysisRelateData.getIntValue("maxSize");
        //指标值
        Map<String, JSONObject> relateAnalysis = new HashMap();
        JSONArray dataListRelate = subTargetAnalysisRelateData.getJSONArray("dataList");
        for (int i = 0; i < dataListRelate.size(); i++) {
            JSONObject jsonObject1 = dataListRelate.getJSONObject(i);
            relateAnalysis.put(jsonObject1.getString("name"), jsonObject1);
        }

        JSONObject chart = new JSONObject();
        JSONArray indicator = new JSONArray();
        JSONObject indicatorJSONObject1 = new JSONObject();
        indicatorJSONObject1.put("name", "静态分析");
        indicator.add(indicatorJSONObject1);
        JSONObject indicatorJSONObject2 = new JSONObject();
        indicatorJSONObject2.put("name", "规律分析");
        indicator.add(indicatorJSONObject2);
        JSONObject indicatorJSONObject3 = new JSONObject();
        indicatorJSONObject3.put("name", "动态分析");
        indicator.add(indicatorJSONObject3);
        JSONObject indicatorJSONObject4 = new JSONObject();
        indicatorJSONObject4.put("name", "意图分析");
        indicator.add(indicatorJSONObject4);
        JSONObject indicatorJSONObject5 = new JSONObject();
        indicatorJSONObject5.put("name", "关联分析");
        indicator.add(indicatorJSONObject5);
        chart.put("indicator", indicator);

        JSONArray seriesData = new JSONArray();

        List<JSONObject> totalScore = new ArrayList<>();
        m4.forEach((k, v) -> {
            JSONObject staticAnalysisJSONObject = staticAnalysis.get(v);
            double staticAnalysisScore = staticAnalysisJSONObject.getDoubleValue("score");
            JSONObject regularAnalysisJSONObject = regularAnalysis.get(v);
            double regularAnalysisScore = regularAnalysisJSONObject.getDoubleValue("score");
            JSONObject dynamicAnalysisJSONObject = dynamicAnalysis.get(v);
            double dynamicAnalysisScore = dynamicAnalysisJSONObject.getDoubleValue("score");
            JSONObject intentAnalysisJSONObject = intentAnalysis.get(v);
            double intentAnalysisScore = intentAnalysisJSONObject.getDoubleValue("score");
            JSONObject relateAnalysisJSONObject = relateAnalysis.get(v);
            double relateAnalysisScore = relateAnalysisJSONObject.getDoubleValue("score");
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", v);
            jsonObject1.put("value", Arrays.asList(staticAnalysisScore, regularAnalysisScore, dynamicAnalysisScore, intentAnalysisScore, relateAnalysisScore));
            seriesData.add(jsonObject1);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("id", k);
            jsonObject2.put("name", v);
            jsonObject2.put("score", staticAnalysisScore + regularAnalysisScore + dynamicAnalysisScore + intentAnalysisScore + relateAnalysisScore);
            totalScore.add(jsonObject2);
        });
        chart.put("seriesData", seriesData);
        Collections.sort(totalScore, Comparator.comparing(j -> j.getDoubleValue("score")));
        Collections.reverse(totalScore);

        JSONObject table = new JSONObject();
        JSONArray header = new JSONArray();
        JSONObject header1 = new JSONObject();
        header1.put("name", "子目标");
        header1.put("child", new ArrayList<>());
        header.add(header1);
        JSONObject header2 = new JSONObject();
        header2.put("name", "静态分析");
        JSONArray staticAnalysisHeader = new JSONArray();
        for (int i = 0; i < indexStatic.size(); i++) {
            staticAnalysisHeader.add(indexStatic.getString(i));
        }
        staticAnalysisHeader.add("分值");
        header2.put("child", staticAnalysisHeader);
        header.add(header2);
        JSONObject header3 = new JSONObject();
        header3.put("name", "规律分析");
        header3.put("child", Arrays.asList("历史停驻飞机", "驻停位置得分", "近三年演习次数得分", "分值"));
        header.add(header3);
        JSONObject header4 = new JSONObject();
        header4.put("name", "动态分析");
        header4.put("child", Arrays.asList("停驻机型", "分值"));
        header.add(header4);
        JSONObject header5 = new JSONObject();
        header5.put("name", "意图分析");
        header5.put("child", Arrays.asList("分值"));
        header.add(header5);
        JSONObject header6 = new JSONObject();
        header6.put("name", "关联分析");
        JSONArray relateAnalysisHeader = new JSONArray();
        for (int i = 1; i <= maxSize; i++) {
            relateAnalysisHeader.add("关联子目标" + i);
            relateAnalysisHeader.add("关联子目标" + i + "得分");
        }
        relateAnalysisHeader.add("分值");
        header6.put("child", relateAnalysisHeader);
        header.add(header6);
        JSONObject header7 = new JSONObject();
        header7.put("name", "分值");
        header7.put("child", new ArrayList<>());
        header.add(header7);
        JSONObject header8 = new JSONObject();
        header8.put("name", "排序");
        header8.put("child", new ArrayList<>());
        header.add(header8);

        JSONArray dataList = new JSONArray();
        for (int i = 0; i < totalScore.size(); i++) {
            JSONObject jsonObject1 = totalScore.get(i);
            JSONArray tableI = new JSONArray();

            String id = jsonObject1.getString("id");
            String name = jsonObject1.getString("name");
            tableI.add(name);

            //静态分析
            JSONObject staticAnalysisJSONObject = staticAnalysis.get(name);
            for (int j = 0; j < indexStatic.size(); j++) {
                tableI.add(staticAnalysisJSONObject.getDoubleValue(indexStatic.getString(i)));
            }
            tableI.add(staticAnalysisJSONObject.getDoubleValue("score"));

            //规律分析
            JSONObject regularAnalysisJSONObject = regularAnalysis.get(name);
            tableI.add(regularAnalysisJSONObject.getString("stay_target"));
            tableI.add(regularAnalysisJSONObject.getString("stay_score"));
            tableI.add(regularAnalysisJSONObject.getString("exercise_score"));
            tableI.add(regularAnalysisJSONObject.getDoubleValue("score"));

            //动态分析
            JSONObject dynamicAnalysisJSONObject = dynamicAnalysis.get(name);
            tableI.add(dynamicAnalysisJSONObject.getString("stay_target"));
            tableI.add(dynamicAnalysisJSONObject.getDoubleValue("score"));

            //意图分析
            JSONObject intentAnalysisJSONObject = intentAnalysis.get(name);
            tableI.add(intentAnalysisJSONObject.getDoubleValue("score"));

            //关联分析
            JSONObject relateAnalysisJSONObject = relateAnalysis.get(name);
            List<JSONObject> jsonObjects = (List<JSONObject>) relateMap.get(id);
            int size = jsonObjects.size();
            for (int j = 0; j < maxSize; j++) {
                if (j + 1 > size) {
                    tableI.add("");
                    tableI.add("");
                } else {
                    JSONObject object = jsonObjects.get(j);
                    tableI.add(object.getString("name"));
                    double score = object.getDoubleValue("score");
                    tableI.add(score);
                }
            }
            tableI.add(relateAnalysisJSONObject.getDoubleValue("score"));

            tableI.add(jsonObject1.getDoubleValue("score"));
            tableI.add(i + 1);

            dataList.add(tableI);
        }
        table.put("header", header);
        table.put("dataList", dataList);
        Map r = new HashMap();
        r.put("chart", chart);
        r.put("table", table);
        return ResultUtil.success(r);
    }

    @Override
    public Result getSubTargetAnalysisRegularList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        JSONArray paramList = jsonObject.getJSONArray("paramList");

        JSONObject subTargetAnalysisRegularData = getSubTargetAnalysisRegularData(jsonObject);
//        JSONObject subTargetAnalysisRegularData;
//        SubTargetAnalysisRegular subTargetAnalysisRegular = subTargetAnalysisRegularService.getOne(new LambdaQueryWrapper<SubTargetAnalysisRegular>()
//                .eq(SubTargetAnalysisRegular::getTargetId, targetId)
//                .eq(SubTargetAnalysisRegular::getParam, JSON.toJSONString(paramList, SerializerFeature.SortField))
//                .isNotNull(SubTargetAnalysisRegular::getContent)
//        );
//        if (subTargetAnalysisRegular == null) {
//            subTargetAnalysisRegularData = getSubTargetAnalysisRegularData(jsonObject);
//        } else {
//            subTargetAnalysisRegularData = JSON.parseObject(subTargetAnalysisRegular.getContent());
//        }

        JSONObject chart = subTargetAnalysisRegularData.getJSONObject("chart");
        List<JSONObject> jsonArray = new ArrayList<>();
        JSONArray dataList = subTargetAnalysisRegularData.getJSONArray("dataList");
        for (int i = 0; i < dataList.size(); i++) {
            jsonArray.add(dataList.getJSONObject(i));
        }

        jsonArray.sort(Comparator.comparing(j -> j.getDoubleValue("score")));
        Collections.reverse(jsonArray);

        JSONArray table = new JSONArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.get(i);
            object.put("sort", i + 1);
            table.add(object);
        }

        Map r = new HashMap();
        r.put("chart", chart);
        r.put("table", table);
        return ResultUtil.success(r);
    }

    private JSONObject getSubTargetAnalysisRegularData(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        JSONArray paramList = jsonObject.getJSONArray("paramList");

        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));
        //目标分数
        Map<String, Double> m = new HashMap();
        //飞机数量
        Map<String, Integer> m2 = new HashMap();
        //飞机id
        Map<String, Set<String>> m3 = new HashMap();
        //子目标名
        Map<String, String> m4 = new HashMap();
        //飞机名
        Map<String, String> map = new HashMap();
        List<PostureType> postureTypeList = postureTypeMapper.selectList(new LambdaQueryWrapper<>());
        postureTypeList.stream()
                .forEach(postureType -> map.put(postureType.getTypeId(), postureType.getTypeName()));
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();
            Set<String> s = new HashSet();
            m.put(subId, 0D);
            m3.put(subId, s);
            m4.put(subId, targetName);
        }

        for (int i = 0; i < paramList.size(); i++) {
            JSONObject jsonObject1 = paramList.getJSONObject(i);
            String stay_id = jsonObject1.getString("stay_id");
            int num = jsonObject1.getIntValue("num");
            if (m2.containsKey(stay_id)) {
                m2.put(stay_id, m2.get(stay_id) + num);
            } else {
                m2.put(stay_id, num);
            }
            String sub_id = jsonObject1.getString("sub_id");
            if (m.containsKey(sub_id)) {
                m.put(sub_id, m.get(sub_id) + postureComputeTypeService.computePostureIdValue(stay_id, num));
            }
            if (m3.containsKey(sub_id)) {
                Set<String> strings = m3.get(sub_id);
                strings.add(stay_id);
                m3.put(sub_id, strings);
            }
        }

        JSONObject chart = new JSONObject();
        JSONArray xAxis = new JSONArray();
        JSONArray yAxis = new JSONArray();
        m2.forEach((k, v) -> {
            xAxis.add(map.get(k));
            yAxis.add(v);
        });
        chart.put("xAxis", xAxis);
        chart.put("yAxis", yAxis);

        List<JSONObject> jsonArray = new ArrayList<>();
        m4.forEach((k, v) -> {
            JSONObject tableJSONObject = new JSONObject();
            tableJSONObject.put("target_id", k);
            tableJSONObject.put("target_name", v);
            String collect;
            if (m3.containsKey(k)) {
                Set<String> strings = m3.get(k);
                collect = strings.stream().map(i -> map.get(i)).collect(Collectors.joining(","));
            } else {
                collect = "";
            }
            tableJSONObject.put("stay_target", collect);
            tableJSONObject.put("stay_score", m.getOrDefault(k, 0D));
            tableJSONObject.put("exercise_score", 0);
            tableJSONObject.put("score", m.getOrDefault(k, 0D));
            jsonArray.add(tableJSONObject);
        });

        SubTargetAnalysisRegular subTargetAnalysisRegular = new SubTargetAnalysisRegular();
        subTargetAnalysisRegular.setTargetId(targetId);
        subTargetAnalysisRegular.setParam(JSON.toJSONString(paramList, SerializerFeature.SortField));
        JSONObject content = new JSONObject();
        content.put("chart", chart);
        content.put("dataList", jsonArray);
        subTargetAnalysisRegular.setContent(content.toJSONString());
        subTargetAnalysisRegularService.saveOrUpdate(subTargetAnalysisRegular, new LambdaUpdateWrapper<SubTargetAnalysisRegular>()
                .eq(SubTargetAnalysisRegular::getTargetId, targetId));
        return content;
    }

    @Override
    public Result getSubTargetAnalysisDynamicList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        JSONArray paramList = jsonObject.getJSONArray("paramList");

        JSONObject subTargetAnalysisDynamicData = getSubTargetAnalysisDynamicData(jsonObject);
//        JSONObject subTargetAnalysisDynamicData;
//        SubTargetAnalysisDynamic subTargetAnalysisDynamic = subTargetAnalysisDynamicService.getOne(new LambdaQueryWrapper<SubTargetAnalysisDynamic>()
//                .eq(SubTargetAnalysisDynamic::getTargetId, targetId)
//                .eq(SubTargetAnalysisDynamic::getParam, JSON.toJSONString(paramList, SerializerFeature.SortField))
//                .isNotNull(SubTargetAnalysisDynamic::getContent)
//        );
//        if (subTargetAnalysisDynamic == null) {
//            subTargetAnalysisDynamicData = getSubTargetAnalysisDynamicData(jsonObject);
//        } else {
//            subTargetAnalysisDynamicData = JSON.parseObject(subTargetAnalysisDynamic.getContent());
//        }

        JSONObject chart = subTargetAnalysisDynamicData.getJSONObject("chart");
        List<JSONObject> jsonArray = new ArrayList<>();
        JSONArray dataList = subTargetAnalysisDynamicData.getJSONArray("dataList");
        for (int i = 0; i < dataList.size(); i++) {
            jsonArray.add(dataList.getJSONObject(i));
        }

        jsonArray.sort(Comparator.comparing(j -> j.getDoubleValue("score")));
        Collections.reverse(jsonArray);

        JSONArray table = new JSONArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.get(i);
            object.put("sort", i + 1);
            table.add(object);
        }

        Map r = new HashMap();
        r.put("chart1", chart);
        r.put("chart2", chart);
        r.put("table", table);
        return ResultUtil.success(r);
    }

    private JSONObject getSubTargetAnalysisDynamicData(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        JSONArray paramList = jsonObject.getJSONArray("paramList");

        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));
        //目标分数
        Map<String, Double> m = new HashMap();
        //飞机数量
        Map<String, Integer> m2 = new HashMap();
        //飞机id
        Map<String, Set<String>> m3 = new HashMap();
        //子目标名
        Map<String, String> m4 = new HashMap();
        //飞机名
        Map<String, String> map = new HashMap();
        List<PostureType> postureTypeList = postureTypeMapper.selectList(new LambdaQueryWrapper<>());
        postureTypeList.stream()
                .forEach(postureType -> map.put(postureType.getTypeId(), postureType.getTypeName()));
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();
            Set<String> s = new HashSet();
            m.put(subId, 0D);
            m3.put(subId, s);
            m4.put(subId, targetName);
        }

        for (int i = 0; i < paramList.size(); i++) {
            JSONObject jsonObject1 = paramList.getJSONObject(i);
            String stay_id = jsonObject1.getString("stay_id");
            int num = jsonObject1.getIntValue("num");
            if (m2.containsKey(stay_id)) {
                m2.put(stay_id, m2.get(stay_id) + num);
            } else {
                m2.put(stay_id, num);
            }
            String sub_id = jsonObject1.getString("sub_id");
            if (m.containsKey(sub_id)) {
                m.put(sub_id, m.get(sub_id) + postureComputeTypeService.computePostureIdValue(stay_id, num));
            }
            if (m3.containsKey(sub_id)) {
                Set<String> strings = m3.get(sub_id);
                strings.add(stay_id);
                m3.put(sub_id, strings);
            }
        }

        JSONObject chart = new JSONObject();
        JSONArray xAxis = new JSONArray();
        JSONArray yAxis = new JSONArray();
        m2.forEach((k, v) -> {
            xAxis.add(map.get(k));
            yAxis.add(v);
        });
        chart.put("xAxis", xAxis);
        chart.put("yAxis", yAxis);

        List<JSONObject> jsonArray = new ArrayList<>();
        m4.forEach((k, v) -> {
            JSONObject tableJSONObject = new JSONObject();
            tableJSONObject.put("target_id", k);
            tableJSONObject.put("target_name", v);
            String collect;
            if (m3.containsKey(k)) {
                Set<String> strings = m3.get(k);
                collect = strings.stream().map(i -> map.get(i)).collect(Collectors.joining(","));
            } else {
                collect = "";
            }
            tableJSONObject.put("stay_target", collect);
            tableJSONObject.put("score", m.getOrDefault(k, 0D));
            jsonArray.add(tableJSONObject);
        });

        SubTargetAnalysisDynamic subTargetAnalysisDynamic = new SubTargetAnalysisDynamic();
        subTargetAnalysisDynamic.setTargetId(targetId);
        subTargetAnalysisDynamic.setParam(JSON.toJSONString(paramList, SerializerFeature.SortField));
        JSONObject content = new JSONObject();
        content.put("chart", chart);
        content.put("dataList", jsonArray);
        subTargetAnalysisDynamic.setContent(content.toJSONString());
        subTargetAnalysisDynamicService.saveOrUpdate(subTargetAnalysisDynamic, new LambdaUpdateWrapper<SubTargetAnalysisDynamic>()
                .eq(SubTargetAnalysisDynamic::getTargetId, targetId));
        return content;
    }

    @Override
    public Result getSubTargetAnalysisIntentList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        JSONArray paramList = jsonObject.getJSONArray("paramList");

        JSONObject subTargetAnalysisIntentData = getSubTargetAnalysisIntentData(jsonObject);
//        JSONObject subTargetAnalysisIntentData;
//        SubTargetAnalysisIntent subTargetAnalysisIntent = subTargetAnalysisIntentService.getOne(new LambdaQueryWrapper<SubTargetAnalysisIntent>()
//                .eq(SubTargetAnalysisIntent::getTargetId, targetId)
//                .eq(SubTargetAnalysisIntent::getParam, JSON.toJSONString(paramList, SerializerFeature.SortField))
//                .isNotNull(SubTargetAnalysisIntent::getContent)
//        );
//        if (subTargetAnalysisIntent == null) {
//            subTargetAnalysisIntentData = getSubTargetAnalysisIntentData(jsonObject);
//        } else {
//            subTargetAnalysisIntentData = JSON.parseObject(subTargetAnalysisIntent.getContent());
//        }

        List<JSONObject> jsonArray = new ArrayList<>();
        JSONArray dataList = subTargetAnalysisIntentData.getJSONArray("dataList");
        for (int i = 0; i < dataList.size(); i++) {
            jsonArray.add(dataList.getJSONObject(i));
        }

        jsonArray.sort(Comparator.comparing(j -> j.getDoubleValue("score")));
        Collections.reverse(jsonArray);

        JSONArray table = new JSONArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.get(i);
            object.put("sort", i + 1);
            table.add(object);
        }

        Map r = new HashMap();
        r.put("table", table);
        return ResultUtil.success(r);
    }

    private JSONObject getSubTargetAnalysisIntentData(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        JSONArray paramList = jsonObject.getJSONArray("paramList");

        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));

        //目标分数
        Map<String, Double> m = new HashMap();
        //子目标名
        Map<String, String> m4 = new HashMap();
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();
            m4.put(subId, targetName);
        }

        for (int i = 0; i < paramList.size(); i++) {
            JSONObject jsonObject1 = paramList.getJSONObject(i);
            String sub_id = jsonObject1.getString("sub_id");
            double num = jsonObject1.getDoubleValue("num");
            m.put(sub_id, num);
        }

        List<JSONObject> jsonArray = new ArrayList<>();
        m4.forEach((k, v) -> {
            JSONObject tableJSONObject = new JSONObject();
            tableJSONObject.put("target_id", k);
            tableJSONObject.put("target_name", v);
            tableJSONObject.put("score", m.getOrDefault(k, 0D));
            jsonArray.add(tableJSONObject);
        });

        SubTargetAnalysisIntent subTargetAnalysisIntent = new SubTargetAnalysisIntent();
        subTargetAnalysisIntent.setTargetId(targetId);
        subTargetAnalysisIntent.setParam(JSON.toJSONString(paramList, SerializerFeature.SortField));
        JSONObject content = new JSONObject();
        content.put("dataList", jsonArray);
        subTargetAnalysisIntent.setContent(content.toJSONString());
        subTargetAnalysisIntentService.saveOrUpdate(subTargetAnalysisIntent, new LambdaUpdateWrapper<SubTargetAnalysisIntent>()
                .eq(SubTargetAnalysisIntent::getTargetId, targetId));
        return content;
    }

    @Override
    public Result getSubTargetAnalysisRelateList(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");

        JSONObject subTargetAnalysisRelateData = getSubTargetAnalysisRelateData(jsonObject);
//        JSONObject subTargetAnalysisRelateData;
//        SubTargetAnalysisRelate subTargetAnalysisRelate = subTargetAnalysisRelateService.getOne(new LambdaQueryWrapper<SubTargetAnalysisRelate>()
//                .eq(SubTargetAnalysisRelate::getTargetId, targetId)
//                .isNotNull(SubTargetAnalysisRelate::getContent)
//        );
//        if (subTargetAnalysisRelate == null) {
//            subTargetAnalysisRelateData = getSubTargetAnalysisRelateData(jsonObject);
//        } else {
//            subTargetAnalysisRelateData = JSON.parseObject(subTargetAnalysisRelate.getContent());
//        }

        int maxSize = subTargetAnalysisRelateData.getIntValue("maxSize");
        List<JSONObject> jsonArray = new ArrayList<>();
        JSONArray dataList = subTargetAnalysisRelateData.getJSONArray("dataList");
        for (int i = 0; i < dataList.size(); i++) {
            jsonArray.add(dataList.getJSONObject(i));
        }
        JSONObject relateMap = subTargetAnalysisRelateData.getJSONObject("relateMap");

        jsonArray.sort(Comparator.comparing(j -> j.getDoubleValue("score")));
        Collections.reverse(jsonArray);

        JSONArray table = new JSONArray();
        JSONArray table0 = new JSONArray();
        table0.add("子目标");
        for (int i = 1; i <= maxSize; i++) {
            table0.add("关联子目标" + i);
            table0.add("关联子目标" + i + "得分");
        }
        table0.add("分值");
        table0.add("排序");
        table.add(table0);

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object1 = jsonArray.get(i);
            String k = object1.getString("id");
            String v = object1.getString("name");
            JSONArray tableI = new JSONArray();
            tableI.add(v);
            int size;
            List<JSONObject> jsonObjects;
            if (relateMap.containsKey(k)) {
                jsonObjects = (List<JSONObject>) relateMap.get(k);
                size = jsonObjects.size();
            } else {
                size = 0;
                jsonObjects = new ArrayList<>();
            }

            for (int j = 0; j < maxSize; j++) {
                if (j + 1 > size) {
                    tableI.add("");
                    tableI.add("");
                } else {
                    JSONObject object = jsonObjects.get(j);
                    tableI.add(object.getString("name"));
                    double score = object.getDoubleValue("score");
                    tableI.add(score);
                }
            }
            tableI.add(object1.getDoubleValue("score"));
            tableI.add(i + 1);
            table.add(tableI);
        }

        Map<String, Object> map = targetService.selectSubRelationShip(targetId);

        Map r = new HashMap();
        r.put("chart", map);
        r.put("table", table);
        return ResultUtil.success(r);
    }

    private JSONObject getSubTargetAnalysisRelateData(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");

        List<TargetSubRelationTable> targetSubRelationTableList = targetSubRelationTableMapper.selectList(new LambdaQueryWrapper<TargetSubRelationTable>()
                .eq(TargetSubRelationTable::getTargetId, targetId));

        //子目标名
        Map<String, String> m4 = new HashMap();
        //目标分数
        Map<String, Double> m2 = new HashMap();
        for (TargetSubRelationTable targetSubRelationTable : targetSubRelationTableList) {
            String subId = targetSubRelationTable.getSubId();
            String targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subId, "targetName").toString();
            m4.put(subId, targetName);
            m2.put(subId, targetSubRelationTable.getIndexValue());
        }

        //关联分数
        Map<String, List<JSONObject>> relateMap = new HashMap();
        List<TargetRelationship> targetRelationshipList = targetRelationshipMapper.selectList(new LambdaQueryWrapper<TargetRelationship>()
                .eq(TargetRelationship::getTargetId, targetId));
        for (TargetRelationship targetRelationship : targetRelationshipList) {
            String point = targetRelationship.getPoint();
            String subOne = targetRelationship.getSubOne();
            Object targetName = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subOne, "targetName");
            String subOneName = targetName.toString();
            String subTwo = targetRelationship.getSubTwo();
            Object targetName1 = redisUtil.hget(RedisName.TARGET_BASE_DATA_ALL + "_" + subTwo, "targetName");
            String subTwoName = targetName1.toString();
            if ("目标1指向目标2".equals(point)) {
                JSONObject object = new JSONObject();
                object.put("name", subTwoName);
                object.put("score", m2.getOrDefault(subTwo, 0D));
                List<JSONObject> jsonObjects;
                if (relateMap.containsKey(subTwo)) {
                    jsonObjects = relateMap.get(subTwo);
                } else {
                    jsonObjects = new ArrayList<>();
                }
                jsonObjects.add(object);
                relateMap.put(subOne, jsonObjects);
            } else if ("目标2指向目标1".equals(point)) {
                JSONObject object = new JSONObject();
                object.put("name", subOneName);
                object.put("score", m2.getOrDefault(subOne, 0D));
                List<JSONObject> jsonObjects;
                if (relateMap.containsKey(subOne)) {
                    jsonObjects = relateMap.get(subOne);
                } else {
                    jsonObjects = new ArrayList<>();
                }
                jsonObjects.add(object);
                relateMap.put(subTwo, jsonObjects);
            }
        }
        //用于计算列数
        int maxSize = 0;
        //目标分数
        List<JSONObject> jsonArray = new ArrayList<>();
        for (Map.Entry<String, String> entry : m4.entrySet()) {
            String k = entry.getKey();
            String v = entry.getValue();

            double scoreTotal = 0;
            int size = 0;
            if (relateMap.containsKey(k)) {
                List<JSONObject> jsonObjects = relateMap.get(k);
                size = jsonObjects.size();
                for (int i = 0; i < size; i++) {
                    scoreTotal += jsonObjects.get(i).getDoubleValue("score");
                }
            }

            JSONObject m1 = new JSONObject();
            m1.put("id", k);
            m1.put("name", v);
            m1.put("score", scoreTotal);
            jsonArray.add(m1);
            maxSize = getMax(maxSize, size);
        }

        JSONObject insert = new JSONObject();
        insert.put("relateMap", relateMap);
        insert.put("dataList", jsonArray);
        insert.put("maxSize", maxSize);
        SubTargetAnalysisRelate subTargetAnalysisRelate = new SubTargetAnalysisRelate();
        subTargetAnalysisRelate.setTargetId(targetId);
        subTargetAnalysisRelate.setContent(insert.toJSONString());
        subTargetAnalysisRelateService.saveOrUpdate(subTargetAnalysisRelate, new LambdaUpdateWrapper<SubTargetAnalysisRelate>().eq(SubTargetAnalysisRelate::getTargetId, targetId));
        return insert;
    }

    @Override
    public Result getParam(JSONObject jsonObject) {
        String targetId = jsonObject.getString("targetId");
        Map returnMap = new HashMap();

        //交战坐标
        String fightPosition;
        TargetAnalyseTable targetAnalyseTable = targetAnalyseTableMapper.selectOne(new LambdaQueryWrapper<TargetAnalyseTable>()
                .eq(TargetAnalyseTable::getRedId, targetId));
        if (null != targetAnalyseTable) {
            fightPosition = targetAnalyseTable.getFightPosition();
        } else {
            fightPosition = "";
        }
        returnMap.put("position", fightPosition);

        //规律分析入参
        JSONArray jsonArrayRegular;
        SubTargetAnalysisRegular subTargetAnalysisRegular = subTargetAnalysisRegularMapper.selectOne(new LambdaQueryWrapper<SubTargetAnalysisRegular>()
                .eq(SubTargetAnalysisRegular::getTargetId, targetId));
        if (null != subTargetAnalysisRegular) {
            jsonArrayRegular = JSON.parseArray(subTargetAnalysisRegular.getParam());
        } else {
            jsonArrayRegular = new JSONArray();
        }
        returnMap.put("paramListRegular", jsonArrayRegular);

        //动态分析入参
        JSONArray jsonArrayDynamic;
        SubTargetAnalysisDynamic subTargetAnalysisDynamic = subTargetAnalysisDynamicMapper.selectOne(new LambdaQueryWrapper<SubTargetAnalysisDynamic>()
                .eq(SubTargetAnalysisDynamic::getTargetId, targetId));
        if (null != subTargetAnalysisDynamic) {
            jsonArrayDynamic = JSON.parseArray(subTargetAnalysisDynamic.getParam());
        } else {
            jsonArrayDynamic = new JSONArray();
        }
        returnMap.put("paramListDynamic", jsonArrayDynamic);

        //意图分析入参
        JSONArray jsonArrayIntent;
        SubTargetAnalysisIntent subTargetAnalysisIntent = subTargetAnalysisIntentMapper.selectOne(new LambdaQueryWrapper<SubTargetAnalysisIntent>()
                .eq(SubTargetAnalysisIntent::getTargetId, targetId));
        if (null != subTargetAnalysisIntent) {
            jsonArrayIntent = JSON.parseArray(subTargetAnalysisIntent.getParam());
        } else {
            jsonArrayIntent = new JSONArray();
        }
        returnMap.put("paramListIntent", jsonArrayIntent);

        return ResultUtil.success(returnMap);
    }

    private int getMax(int i1, int i2) {
        return i1 > i2 ? i1 : i2;
    }

    private List<PostureTypeVO> getChildren(String parentId, List<PostureType> postureTypeList) {
        List<PostureTypeVO> collect = postureTypeList.stream()
                .filter(postureType -> postureType.getParentId().equals(parentId))
                .map(postureType -> {
                    PostureTypeVO postureTypeVO = new PostureTypeVO();
                    String parentIdSub = postureType.getTypeId();
                    postureTypeVO.setId(parentIdSub);
                    postureTypeVO.setName(postureType.getTypeName());
                    postureTypeVO.setChildren(getChildren(parentIdSub, postureTypeList));
                    return postureTypeVO;
                }).collect(Collectors.toList());
        return collect;
    }
}
